
//1577.数的平方等于两数乘积的方法数
class Solution {
    int ret;
    void twosum(vector<int>& nums,long long target)
    {
        int n=nums.size();
        int left=0,right=n-1;
        while(left<right)
        {
            if((long long)nums[left]*nums[right]>target) right--;
            else if((long long)nums[left]*nums[right]<target) left++;
            else 
            {
                int l=1,tmp=left+1;
                while(tmp<right&&nums[tmp]==nums[tmp-1]) l++,tmp++;  //计算left有多少个相同的数
                ret+=l;
                right--;
            }
        }
    }

public:
    int numTriplets(vector<int>& nums1, vector<int>& nums2) {
        //依旧是两数之和题型
        //遍历nums1，将nums[i]^2作为target在nums2中找满足的个数
        sort(nums1.begin(),nums1.end());
        sort(nums2.begin(),nums2.end());

        int n1=nums1.size(),n2=nums2.size();
        ret=0;
        for(int i=0;i<n1;i++)
            twosum(nums2,(long long)nums1[i]*nums1[i]);  //进行两数求和
        
        for(int i=0;i<n2;i++)
            twosum(nums1,(long long)nums2[i]*nums2[i]);
        
        return ret;
    }
};